﻿package com.schimanek.navigation.cloud 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class Cloud extends Sprite 
	{
		private var aItems		:Array;
		private var aCloudItems	:Array;
		private var radius		:Number;
		private var xRot		:Number = 0;
		private var yRot		:Number = 0;
		/**
		 * The Alpha Value for the most distant Objects
		 */
		public static var minAlpha	:Number = .2;
		
		/**
 		 *  Divides Rotation-Increment around X axis
		 */
		public static var rotationXDivider:Number = 70;
		
		/**
		 *  Divides Rotation-Increment around Y axis
		 */
		public static var rotationYDivider:Number = 70;
		
		/**
		 * 
		 * @param	items An Array of DisplayObjects
		 * @param   r The Radius of the Cloud
		 */
		public function Cloud(items:Array, r:Number) 
		{
			radius = r;
			aItems = items;
			generateCloud();
			
			//update();
			//render();
		}
		
		public function update(e:Event):void
		{
			//xRot+=0.05;
			//yRot+=0.05;
			xRot = mouseY / rotationXDivider;
			yRot = mouseX / rotationYDivider;
			render();
		}
		
		private function render():void
		{
			var count:uint = aCloudItems.length;
			while (count > 0) {
				count--;
				var cItem:CloudItem = aCloudItems[count] as CloudItem;
				
				cItem.num3d.rotateY(-yRot);
				cItem.num3d.rotateX(xRot);
				
				cItem.x = cItem.num3d.x;
				cItem.y = cItem.num3d.y;
				cItem.z3d = cItem.num3d.z;
				//trace((radius + cItem.num3d.z) / (2*radius));
				cItem.alpha = minAlpha + (1*radius + cItem.num3d.z) / (2*radius)*(1-minAlpha);
				//trace(cItem.alpha);
				cItem.scaleX = 1+(radius+cItem.num3d.z)/radius;
				cItem.scaleY = 1+(radius+cItem.num3d.z)/radius;
			}
			arrangeItems();
		}
		
		private function arrangeItems():void
		{
			aCloudItems.sortOn("z3d", Array.NUMERIC);
			var i:int = aCloudItems.length;
			while(i--){
				if (getChildIndex(aCloudItems[i]) != i) {
					setChildIndex(aCloudItems[i], i);
				}
			}
		}
		
		private function generateCloud():void
		{
			aCloudItems = new Array();
			for (var i:int = 1; i <= aItems.length; i++) 
			{
				//trace(i);
				var cItem:CloudItem = new CloudItem();
				
				cItem.phi = Math.acos( -1 +(2 * i - 1) / aItems.length);
				
				//cItem.phi = Math.acos( -1 + (2 * i - 1) / aItems.length);
				//trace(cItem.phi);
				cItem.theta = Math.sqrt(aItems.length*Math.PI)*cItem.phi;

				cItem.addChild(aItems[i-1] as DisplayObject);
				addChild(cItem);
				aCloudItems.push(cItem);
			}
			
			distribute();
		}
		
		public function distribute():void {
			var count:uint = aCloudItems.length;
			while (count > 0) {
				count--;
				var cItem:CloudItem = aCloudItems[count] as CloudItem;
				cItem.x
				
				cItem.num3d.x= radius*Math.cos(cItem.theta+yRot)*Math.sin(cItem.phi+xRot);
				cItem.num3d.y = radius*Math.sin(cItem.theta+yRot)*Math.sin(cItem.phi+xRot);
				cItem.num3d.z = radius * Math.cos(cItem.phi + xRot);
				//trace(cItem.z3d);
				//cItem.scaleX = 4 + cItem.z3d / 100;
				//cItem.scaleY = 4 + cItem.z3d / 100;
	

			}
		}
		
	}
	
}